﻿using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CloudShot.ImageStorages;
using CloudShot.Properties;
using CloudShot.Utils;

namespace CloudShot
{
	public partial class SettingsForm : Form
	{
		private readonly AppSettings _appSettings;
		private readonly IImageStorage _storage;

		public SettingsForm(AppSettings appSettings, IImageStorage storage)
		{
			_appSettings = appSettings;
			_storage = storage;

			InitializeComponent();
			this.Icon = Resources.applicationIcon;

			chkOpenInBrowser.Checked = _appSettings.OpenInBrowser;
			chkCopyUrl.Checked = _appSettings.CopyUrl;
			chkCopyImage.Checked = _appSettings.CopyImage;
			hkbRegion.Text = _appSettings.ShotHotkey;

			cboImageFormat.Items.AddRange(storage.SupportedFormats);
			cboImageFormat.SelectedItem = GetImageFormat(_appSettings.ImageFormat);

			txtPath.Text = _appSettings.PathToSave;
			txtPath.SelectionStart = txtPath.Text.Length;
			toolTip.SetToolTip(txtPath, txtPath.Text);

			pnlSaveToFolder.Visible = storage.SupportsSaveToFileSystem;

			chkStartWithWindows.Checked = _appSettings.StartWithComputer;

			UpdateIntegrationStatus(_appSettings.PathToSave);
		}

		private void OnOkClick(object sender, EventArgs e)
		{
			if (!Directory.Exists(txtPath.Text))
			{
				MessageBox.Show("Invalid path to save.\nPlease enter correct path.",
					Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
				txtPath.Focus();
				return;
			}

			if (_storage.SupportsSaveToFileSystem)
				_appSettings.PathToSave = txtPath.Text;

			_appSettings.OpenInBrowser = chkOpenInBrowser.Checked;
			_appSettings.CopyUrl = chkCopyUrl.Checked;
			_appSettings.CopyImage = chkCopyImage.Checked;
			_appSettings.ShotHotkey = hkbRegion.Text;

			_appSettings.ImageFormat = GetImageFormat((ImageFormat)cboImageFormat.SelectedItem);

			_appSettings.StartWithComputer = chkStartWithWindows.Checked;

			_appSettings.Save();

			DialogResult = DialogResult.OK;
		}

		private ImageFormat GetImageFormat(ImageFormat selectedImageFormat)
		{
			bool correctImageFormat = false;
			foreach (ImageFormat imageFormat in _storage.SupportedFormats)
			{
				if (imageFormat == selectedImageFormat)
					correctImageFormat = true;
			}

			return correctImageFormat ? selectedImageFormat : _storage.SupportedFormats.FirstOrDefault();
		}

		private void OnBrowseClick(object sender, EventArgs e)
		{
			var dialog = new FolderBrowserDialog();
			dialog.Description = "Select folder to save screenshots";
			dialog.SelectedPath = string.IsNullOrEmpty(txtPath.Text) ? DropboxUtils.DropboxPublicFolder : txtPath.Text;

			if (dialog.ShowDialog() == DialogResult.OK)
			{
				txtPath.Text = dialog.SelectedPath;
				txtPath.SelectionStart = txtPath.Text.Length;
				toolTip.SetToolTip(txtPath, txtPath.Text);

				UpdateIntegrationStatus(txtPath.Text);
			}
		}

		private void OnSavePathChanged(object sender, EventArgs e)
		{
			UpdateIntegrationStatus(txtPath.Text);
		}

		private void UpdateIntegrationStatus(string path)
		{
			//TODO refactor this, we mustn't know about DropboxImageStorage
			if (_storage is DropboxImageStorage && !string.IsNullOrEmpty(path) && path.Contains(DropboxUtils.DropboxPublicFolder))
			{
				lblDropbox.Visible = true;
				//lblDropbox.Text = "Dropbox integration enabled";
			}
			else
			{
				lblDropbox.Visible = false;
			}
		}
	}
}